package com.sgj;

import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

/**
 * Create by sgj on 2020/10/15 15:49
 */
public class Test {

    private static RestHighLevelClient getClient() {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 9200, "http")));
        return client;
    }

    private static void close(RestHighLevelClient client) {
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        RestHighLevelClient client = getClient();

        Map<String, Object> jsonMap = new HashMap<>();

        Connection c = null;
        Statement stmt = null;
        try {
            Class.forName("org.postgresql.Driver");
            c = DriverManager.getConnection(
                    "jdbc:postgresql://47.92.120.172:5432/mh_server",
                    "postgres", "MoHuoEr2016");
            c.setAutoCommit(false);

            stmt = c.createStatement();

            ResultSet rs = stmt.executeQuery("select id,name,invest_objective,compare_benchmarks,invest_scope,short_name," +
                    "establish_date,manager_company,custody_company,opration_type,created_time,updated_time,pinyin,short_pinyin," +
                    "is_display,fund_scale,fund_share from mh_fund");
            int i = 0;
            while(rs.next()){
                ++i;
                String id = rs.getString("id");
                String name = rs.getString("name");
                String invest_objective = rs.getString("invest_objective");
                String compare_benchmarks = rs.getString("compare_benchmarks");
                String invest_scope = rs.getString("invest_scope");
                String short_name = rs.getString("short_name");
                String establish_date = rs.getString("establish_date");
                String manager_company = rs.getString("manager_company");
                String custody_company = rs.getString("custody_company");
                int opration_type = rs.getInt("opration_type");
                String created_time = rs.getString("created_time");
                String updated_time = rs.getString("updated_time");
                String pinyin = rs.getString("pinyin");
                String short_pinyin = rs.getString("short_pinyin");
                int is_display = rs.getInt("is_display");
                float fund_scale = rs.getFloat("fund_scale");
                float fund_share = rs.getFloat("fund_share");
                System.out.println(id + "," + name + "," + invest_objective + "," + compare_benchmarks + "," + invest_scope + "," +
                        short_name + "," + establish_date + "," + manager_company + "," + custody_company + "," + opration_type + "," +
                        created_time + "," + updated_time + "," + pinyin + "," + short_pinyin + "," + is_display + "," + fund_scale + "," + fund_share);
                System.out.println();
                jsonMap.put("id", id);
                jsonMap.put("name", name);
                jsonMap.put("invest_objective", invest_objective);
                jsonMap.put("compare_benchmarks", compare_benchmarks);
                jsonMap.put("invest_scope", invest_scope);
                jsonMap.put("short_name", short_name);
                jsonMap.put("establish_date", establish_date);
                jsonMap.put("manager_company", manager_company);
                jsonMap.put("custody_company", custody_company);
                jsonMap.put("opration_type", opration_type);
                jsonMap.put("created_time", created_time);
                jsonMap.put("updated_time", updated_time);
                jsonMap.put("pinyin", pinyin);
                jsonMap.put("short_pinyin", short_pinyin);
                jsonMap.put("is_display", is_display);
                jsonMap.put("fund_scale", fund_scale);
                jsonMap.put("fund_share", fund_share);
                IndexRequest request = new IndexRequest("mh_fund").id(""+i).source(jsonMap);

                try {
                    IndexResponse response = client.index(request, RequestOptions.DEFAULT);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            rs.close();
            stmt.close();

            c.close();

        } catch (Exception e) {
            e.printStackTrace();
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
            System.exit(0);
        }

        close(client);
    }
}
